LootMe = {}

local version = "0.2.0"

local onLootWindowShown

function LootMe.Initialize()
  --initialize stored settings if not present
  if not LootMeSettings then
    LootMeSettings = {
            isLootMeRunning = false,
            sessionStateStorageEnabled = false,
    }
  end
  
  --set initial state
  if (LootMeSettings.sessionStateStorageEnabled) then
    if (LootMeSettings.isLootMeRunning) then
      LootMe.Enable()
    else
      LootMe.Disable()
    end
  else
    --default to off
    LootMe.Disable()
  end
  
  onLootWindowShown = EA_Window_Loot.OnShown
  EA_Window_Loot.OnShown = LootMe.OnLootWindowShown

  LibSlash.RegisterSlashCmd("lootme", LootMe.HandleSlashCommand)	
end

function LootMe.OnLootWindowShown(...)
  if not LootMeSettings.isLootMeRunning then
    return onLootWindowShown(...)
  end

  -- do stuff here
  EA_Window_Loot.OnLootAll()
  return onLootWindowShown(...)
end

function LootMe.parseArguments(delimiter,str)
  if (delimiter=='') then 
    return false 
  end
  
  local pos = 0
  local arr = {}
  for st,sp in function() return string.find(str,delimiter,pos,true) end do
    table.insert(arr,string.sub(str,pos,st-1)) 
    pos = sp + 1
  end
  table.insert(arr,string.sub(str,pos))
  return arr
end

function LootMe.HandleSlashCommand(input)
  local args = LootMe.parseArguments(" ",input)
  if args[1] == "help" then
    LootMe.DisplayHelp()
  elseif args[1] == "on" then
    LootMe.Enable()
  elseif args[1] == "off" then
    LootMe.Disable()
  elseif args[1] == "status" then
    LootMe.DisplayStatus()
  elseif args[1] == "saveon" then
    LootMe.EnableSessionStateStorage()
  elseif args[1] == "saveoff" then
    LootMe.DisableSessionStateStorage()
  else
    LootMe.DisplayHelp();
  end  
end

function LootMe.DisplayHelp()
  LootMe.WriteToChat("LootMe addon Help")
  LootMe.WriteToChat("Version: "..version)
  LootMe.DisplayStatus()
  LootMe.WriteToChat("Usage: /lootme [arguments]")
  LootMe.WriteToChat("/lootme help - display this help message")
  LootMe.WriteToChat("/lootme [on/off] - turn the LootMe addon on/off")
  LootMe.WriteToChat("/lootme status - display the current status for LootMe")
  LootMe.WriteToChat("/lootme saveon - sets the current state of LootMe (on/off) to be saved across playing sessions")
  LootMe.WriteToChat("/lootme saveoff - sets the current state of LootMe (on/off) to NOT be saved across playing sessions")
end

function LootMe.Enable()
  LootMeSettings.isLootMeRunning = true
  LootMe.WriteToChat("LootMe addon is enabled")
end

function LootMe.Disable()
  LootMeSettings.isLootMeRunning = false
  LootMe.WriteToChat("LootMe addon is disabled")
end

function LootMe.EnableSessionStateStorage()
  LootMeSettings.sessionStateStorageEnabled = true
  LootMe.WriteToChat("LootMe settings will now be saved across playing sessions")
end

function LootMe.DisableSessionStateStorage()
  LootMeSettings.sessionStateStorageEnabled = false
  LootMe.WriteToChat("LootMe settings will NOT be saved across playing sessions")
end

function LootMe.DisplayStatus()
  if LootMeSettings.isLootMeRunning then
    LootMe.WriteToChat("Status: LootMe Enabled")
  else 
    LootMe.WriteToChat("Status: LootMe Disabled")
  end
  
  if LootMeSettings.sessionStateStorageEnabled then
    LootMe.WriteToChat("Session State: Enabled")
  else 
    LootMe.WriteToChat("Session State: Disabled")
  end
end

function LootMe.WriteToChat(text)
  EA_ChatWindow.Print(towstring(text))
end
